<?php

class pastebinNG {
	private $dbh;

	function __construct($dbh) {
		$this->dbh = $dbh;
	}

	function get($id, $formatted = TRUE) {
		$rtv = TRUE;
		$postinfo = $this->get_post($id);
		if($postinfo != FALSE) {
			if($formatted == TRUE) {
				if($postinfo['formatted'] == 1) {
					$res = $this->get_post_body_html($id);
					$css = $this->get_post_body_css($id);
					if($res != FALSE) {
						$body = $res;
					} else {
						$rtv = FALSE;
					}
				} else {
					$res = $this->get_post_body_raw($id);
					if($res != FALSE) {
						$geshi = new GeSHi($res, $postinfo['format']);
						$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
						$geshi->enable_classes();
						$formatted_code = $geshi->parse_code();
						$css = $geshi->get_stylesheet();
						$res_insert = $this->set_post_body_html($id, $formatted_code);
						$res_insert_css = $this->set_post_body_css($id, $css);
						// TODO - Handle CSS Insert Error...
						if($res_insert == TRUE) {
							$sql = "UPDATE `tbl_posts` SET `formatted` = 1 WHERE `post_id` = " . $id . ";";
							$res_update = $this->dbh->query($sql);
							if($res_update != TRUE) {
								$sql = "DELETE `tbl_post_body_html` FROM `tbl_post_body_html WHERE `id` = " . $id . ";";
								$res_delete = $this->dbh->query($sql);
							}
						}
						$body = $formatted_code;
					} else {
						$rtv = FALSE;
					}
				}
				if($rtv == TRUE) {
					$rv = $this->get_post_tags($id);
					if($rv != FALSE) {
						$tags = $rv;
						unset($rv);
					} else {
						$tags = array();
					}
				}
			} else {
				$res = $this->get_post_body_raw($id);
				if($res != FALSE) {
					$body = $res;
					$css = NULL;
				} else {
					$rtv = FALSE;
				}
			}
			if($rtv == TRUE) {
				$rtv = array(
					 'post_id' => $postinfo['post_id']
					,'title' => $postinfo['title']
					,'author' => $postinfo['author']
					,'datetime' => $postinfo['datetime']
					,'format' => $postinfo['format']
					,'tags' => $tags
					,'css' => $css
					,'body' => $body
				);
			}
		} else {
			unset($res);
			$rtv = FALSE;
		}
		return $rtv;
	}

	function get_recent_posts($num = 10) {
		$sql = "SELECT `post_id`, `title`, `author`, `datetime`, `format`, `formatted` FROM `tbl_posts` ORDER BY `datetime` DESC LIMIT 0," . $num . ";";
		$res = $this->dbh->query($sql);
		if(is_object($res) && $res->num_rows() > 0) {
			$rtv = array();
			while($row = $res->next_hash()) {
				$rtv[] = $row;
			}
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function get_tags_pastes($tag) {
		$sql = "SELECT `tbl_posts`.`post_id`, `title`, `author`, `datetime`, `format`, `formatted` FROM `tbl_posts` " . 
			"INNER JOIN `tbl_tag2paste2tag` ON `tbl_tag2paste2tag`.`post_id` = `tbl_posts`.`post_id` " .
			"INNER JOIN `tbl_tags` ON `tbl_tags`.`tag_id` = `tbl_tag2paste2tag`.`tag_id` " .
			"WHERE `tbl_tags`.`tag` = '" . $this->dbh->escape($tag) . "';";
		$res = $this->dbh->query($sql);
		if(is_object($res) && $res->num_rows() > 0) {
			$rtv = array();
			while($row = $res->next_hash()) {
				$rtv[] = $row;
			}
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function get_expired_posts() {
		$sql = "SELECT `post_id` FROM `tbl_posts` WHERE `expire` > 0 AND `expire` <= " . time() . ";";
		$res = $this->dbh->query($sql);
		if(is_object($res) && $res->num_rows() > 0) {
			$rtv = array();
			while($row = $res->next_hash()) {
				$rtv[] = $row['post_id'];
			}
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function get_post($id)
	{
		$sql = "SELECT `post_id`, `title`, `author`, `datetime`, `format`, `formatted` FROM `tbl_posts` WHERE `tbl_posts`.`post_id` = " . $id . ";";
		$res = $this->dbh->query($sql);
		if(is_object($res) && $res->num_rows() == 1) {
			$rtv = $res->next_hash();
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function get_post_body_raw($id) {
		$sql = "SELECT `body_raw` FROM `tbl_post_body_raw` WHERE `post_id` = " . $id . ";";
		$res = $this->dbh->query($sql);
		if(is_object($res) && $res->num_rows() == 1) {
			$row_body = $res->next_hash();
			$rtv = $row_body['body_raw'];
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function get_post_body_html($id) {
		$sql = "SELECT `body_html` FROM `tbl_post_body_html` WHERE `post_id` = " . $id . ";";
		$res = $this->dbh->query($sql);
		if(is_object($res) && $res->num_rows() == 1) {
			$row_body = $res->next_hash();
			$rtv = $row_body['body_html'];
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function get_post_body_css($id) {
		$sql = "SELECT `body_css` FROM `tbl_post_body_css` WHERE `post_id` = " . $id . ";";
		$res = $this->dbh->query($sql);
		if(is_object($res) && $res->num_rows() == 1) {
			$row_body = $res->next_hash();
			$rtv = $row_body['body_css'];
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function get_post_tags($id) {
		$sql = "SELECT `tag` FROM `tbl_posts` " .
			"INNER JOIN `tbl_tag2paste2tag` ON `tbl_tag2paste2tag`.`post_id` = `tbl_posts`.`post_id` " .
			"INNER JOIN `tbl_tags` ON `tbl_tags`.`tag_id` = `tbl_tag2paste2tag`.`tag_id` " .
			"WHERE `tbl_posts`.`post_id` = " . $id . ";";
		$res = $this->dbh->query($sql);
		if(is_object($res) && $res->num_rows() > 0) {
			$rtv = array();
			while($row = $res->next_hash()) {
				$rtv[] = $row['tag'];
			}
		} else {
			$rtv = FALSE;
		}
		return $rtv;
		
	}

	function set($title, $author, $format, $body, $expire = "-1", $tags = "") {
		$id = $this->set_post($title, $author, $format, $expire);
		if($id > 0) {
				$res = $this->set_post_body_raw($id, $body);
				if($res == TRUE) {
					$this->set_post_tags($id, $tags);
					$rtv = $id;
				} else {
					$sql = "DELETE `tbl_posts` FROM `tbl_posts` WHERE `post_id` = " . $id . ";";
					$this->dbh->query($sql);
					$rtv = FALSE;
				}
				unset($res);
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function set_post($title, $author, $format, $expire) {
		if($expire > 0) {
			$expire = time() + $expire;
		}
		$sql = "INSERT INTO `tbl_posts` (`title`, `author`, `datetime`, `format`, `formatted`, `expire`) " . 
			"VALUES ('" . $this->dbh->escape($title) . "', '" . $this->dbh->escape($author) . "', NOW(), '" . $this->dbh->escape($format) . "', 0, " . $expire . ");";
		$rv = $this->dbh->query($sql);
		if($rv == TRUE) {
			$rtv = $this->dbh->insert_id();
		} else {
			$rtv = FALSE;
		}
		return $rtv;
	}

	function set_post_body_raw($id, $body) {
		$sql = "INSERT INTO `tbl_post_body_raw` (`post_id`, `body_raw`) " .
			"VALUES (" . $id . ", '" . $this->dbh->escape($body) . "');";
		return $this->dbh->query($sql);
	}

	function set_post_body_html($id, $body) {
		$sql_insert = "INSERT INTO `tbl_post_body_html` (`post_id`, `body_html`) " .
			"VALUES (" . $id . ", '" . $this->dbh->escape($body) . "');";
		return $this->dbh->query($sql_insert);
	}

	function set_post_body_css($id, $css) {
		$sql_insert = "INSERT INTO `tbl_post_body_css` (`post_id`, `body_css`) " .
			"VALUES (" . $id . ", '" . $this->dbh->escape($css) . "');";
		return $this->dbh->query($sql_insert);
	}

	function set_post_tags($id, $tags) {
		$unq_tags = explode(" ", $tags);
		foreach($unq_tags as $t) {
			$tid = NULL;
			$sql = "SELECT `tag_id` FROM `tbl_tags` WHERE `tag` = '" . $this->dbh->escape($t) . "';";
			$res = $this->dbh->query($sql);
			if(is_object($res) && $res->num_rows() == 1) {
				$row = $res->next_hash();
				$tid = $row['tag_id'];
			} else {
				$sql = "INSERT INTO `tbl_tags` (`tag`) VALUES ('" . $this->dbh->escape($t) . "');";
				$res = $this->dbh->query($sql);
				if($res == TRUE) {
					$tid = $this->dbh->insert_id();
				}
			}
			if($tid > 0) {
				$sql = "INSERT INTO `tbl_tag2paste2tag` (`post_id`, `tag_id`) VALUES (" . $id . ", " . $tid . ");";
				$res = $this->dbh->query($sql);
			}
		}
	}

	function del_post($id) {
		$sql = "DELETE `tbl_posts`, `tbl_post_body_raw`, `tbl_post_body_html`, `tbl_post_body_css` " .
			"FROM `tbl_posts` " .
			"LEFT JOIN `tbl_post_body_raw` ON `tbl_posts`.`post_id` = `tbl_post_body_raw`.`post_id` " .
			"LEFT JOIN `tbl_post_body_html` ON `tbl_posts`.`post_id` = `tbl_post_body_html`.`post_id` " .
			"LEFT JOIN `tbl_post_body_css` ON `tbl_posts`.`post_id` = `tbl_post_body_css`.`post_id` " . 
			"WHERE `tbl_posts`.`post_id` ";
		if(is_array($id)) {
			$sql .= "IN (" . implode(",", $id) . ");";
		} else {
			$sql .= "= " . $id . ";";
		}
		return $this->dbh->query($sql);
	}
	
	function remove_expired_posts() {
		$rv = $this->get_expired_posts();
		if($rv != FALSE) {
			$this->del_post($rv);
		}
	}
}

?>